Corrected version of hw4 in R markdown format:

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(sf)
## Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
library(here)
## here() starts at /Users/hannawojciak/Documents/MScUrbanSpatialScience/Year1/GISS/CASA0005_GitHub/CASA0005/hw_wk4
library(janitor)
## 
## Attaching package: 'janitor'
## 
## The following objects are masked from 'package:stats':
## 
##     chisq.test, fisher.test
install.packages('countrycode')
## 
## The downloaded binary packages are in
##  /var/folders/h5/7j7qytrs0c7b_8rx21sjgnmm0000gn/T//RtmpMZkLry/downloaded_packages
library(countrycode)

Reading in Data

here::here()
## [1] "/Users/hannawojciak/Documents/MScUrbanSpatialScience/Year1/GISS/CASA0005_GitHub/CASA0005/hw_wk4"
WorldRaw <- st_read(here::here("hw_wk4_data",
                                   "World_Countries_(Generalized).geojson"))
## Reading layer `World_Countries_(Generalized)' from data source 
##   `/Users/hannawojciak/Documents/MScUrbanSpatialScience/Year1/GISS/CASA0005_GitHub/CASA0005/hw_wk4/hw_wk4_data/World_Countries_(Generalized).geojson' 
##   using driver `GeoJSON'
## Simple feature collection with 251 features and 5 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -180 ymin: -89 xmax: 180 ymax: 83.6236
## Geodetic CRS:  WGS 84
UNDPRaw <- read_csv(here::here("hw_wk4_data",
                               "HDR23-24_Composite_indices_complete_time_series.csv"),
                    locale = locale(encoding = "latin1"),
                       na = " ", skip=0)
## Warning: One or more parsing issues, call `problems()` on your data frame for details,
## e.g.:
##   dat <- vroom(...)
##   problems(dat)
## Rows: 206 Columns: 1076
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr    (4): iso3, country, hdicode, region
## dbl (1072): hdi_rank_2022, hdi_1990, hdi_1991, hdi_1992, hdi_1993, hdi_1994,...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

Cleaning Data

I’m choosing to join the data using country names (rather than country codes)

UNDPcols<- UNDPRaw %>%
  clean_names()%>%
  select(country, gii_2019, gii_2010)%>%
  mutate(difference=gii_2019-gii_2010)

WorldClean <- WorldRaw %>% 
  clean_names()

Joining Data

Join_UNDP_World <- WorldClean %>% 
  left_join(., 
            UNDPcols,
            by = "country")

Creating an Interactive Map

library(leafpop)
library(leaflet)
library(tmap)
## Breaking News: tmap 3.x is retiring. Please test v4, e.g. with
## remotes::install_github('r-tmap/tmap')
library(tmaptools)
library(RColorBrewer)

Removing the geometry for the pop up boxes:

popupUNDP <-Join_UNDP_World %>%
  st_drop_geometry()%>%
  dplyr::select(`difference`, country)%>%
  popupTable()

tmap_mode("view")
## tmap mode set to interactive viewing

Setting breaks and colors:

breaks <- pretty(Join_UNDP_World$`difference`, n = 6)

pal1 <- Join_UNDP_World %>%
  colorBin(palette = "RdBu",domain=.$`difference`, bins=breaks)

map<- leaflet(Join_UNDP_World) %>%
  addPolygons(color="white", 
              weight = 1,
              opacity = 1,
              dashArray = "",
              popup = popupUNDP,
              fillOpacity = 0.7,
              fillColor = ~pal1(`difference`),
              group = "difference")%>%

  addTiles(group = "UNDP (default)") %>%
  addProviderTiles(providers$Stadia.StamenToner, group = "Toner") %>%
  addProviderTiles(providers$Stadia.StamenTonerLite, group = "Toner Lite") %>%
  addProviderTiles(providers$CartoDB.Positron, group = "CartoDB")%>%
  
  addLegend(pal = pal1, values = ~`difference`, group = c("difference"), 
            position ="bottomleft", title = "UNDP gii Differnce") %>%
  addLayersControl(
    baseGroups = c("UNDP (default)", "Toner", "Toner Lite", "CartoDB"),
    options = layersControlOptions(collapsed = FALSE)
  )

map